package com.yanqu.road.server.cmd.union.cross.parergon;

import com.yanqu.road.entity.config.parergon.UnionParergonConfig;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.text.MessageFormat;

@Cmd(code = Protocol.S_UNION_PARERGON_FINISH_REWARD, desc = "副业完成奖励")
public class UnionParergonFinishRewardCmdFromCross implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        CrossUnionProto.UnionParergonFinishRewardNotifyMsg rewardNotifyMsg = CrossUnionProto.UnionParergonFinishRewardNotifyMsg.parseFrom(packet.getBytes());

        UnionParergonConfig parergonConfig = UnionConfigMgr.getParergonConfig(rewardNotifyMsg.getParergonId());
        if(parergonConfig == null){

            return;
        }

        for (CrossUnionProto.UnionParergonFinishUserRewardTemp rewardTemp : rewardNotifyMsg.getUserList()) {

            long userId = rewardTemp.getUserId();
            UserInfo userInfo = UserMgr.getUserInfo(userId);
            if(userInfo == null){
                continue;
            }

            String name = MultipleLanguageMgr.getContent(parergonConfig.getName(), userInfo.getLanguage());

            if(rewardTemp.getReward()){

                String mailTitle = MultipleLanguageMgr.getContent(MailManager.UNION_PARERGON_FINISH_TITLE, userInfo.getLanguage());
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.UNION_PARERGON_FINISH_CONTENT, userInfo.getLanguage()), name);
                MailManager.sendMail(userId, eMailType.DAILY.getValue(), parergonConfig.getRewards(), mailContent, mailTitle);

            }else {

                String mailTitle = MultipleLanguageMgr.getContent(MailManager.UNION_PARERGON_CANNOT_FINISH_TITLE, userInfo.getLanguage());
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.UNION_PARERGON_CANNOT_FINISH_CONTENT, userInfo.getLanguage()), name);
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), "", mailContent, mailTitle);

            }

        }

    }
}
